package com.wugh.fast.module.mapper.filter;

import com.wugh.fast.module.mapper.interceptor.plugin.PluginSwitch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 说明: CommonMapper的自定义插件过滤器
 *
 * @author wugh
 * @date 2018-12-02 18:13
 */
@Slf4j
public class CustomPluginsFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            try {
                PluginSwitch.init();
            } catch (Exception e) {
                log.info("自定义插件开关初始化失败:{}", e.getMessage());
            }
            filterChain.doFilter(request, response);
        } finally {
            try {
                PluginSwitch.clear();
            } catch (Exception e) {
                log.info("自定义插件开关状态清空失败失败:{}", e.getMessage());
            }
        }
    }
}
